package com.hdx.server.service.impl;


import com.hdx.common.constant.StatusConstant;
import com.hdx.model.entity.Orders;
import com.hdx.model.vo.BusinessDataVO;
import com.hdx.model.vo.DishOverViewVO;
import com.hdx.model.vo.OrderOverViewVO;
import com.hdx.model.vo.SetmealOverViewVO;
import com.hdx.server.mapper.DishMapper;
import com.hdx.server.mapper.OrderMapper;
import com.hdx.server.mapper.SetmealMapper;
import com.hdx.server.mapper.UserMapper;
import com.hdx.server.service.WorkspaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        /**
         * 营业额：当日已完成订单的总金额
         * 有效订单：当日已完成订单的数量
         * 订单完成率：有效订单数 / 总订单数
         * 平均客单价：营业额 / 有效订单数
         * 新增用户：当日新增用户的数量
         */

        Map map = new HashMap();
        map.put("begin",begin);
        map.put("end",end);

        //查询总订单数
        Integer totalOrderCount = orderMapper.countByMap(map);

        map.put("status", Orders.COMPLETED);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null? 0.0 : turnover;

        //有效订单数
        Integer validOrderCount = orderMapper.countByMap(map);

        Double unitPrice = 0.0;

        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0 && validOrderCount != 0){
            //订单完成率
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount;
            //平均客单价
            unitPrice = turnover / validOrderCount;
        }

        //新增用户数
        Integer newUsers = userMapper.countByMap(map);

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }
    //查询订单管理数据
    @Override
    public OrderOverViewVO getOrderOverViewVO() {
        //新建map对象放入begin和状态为待接单2 根据map查询数据库
        Map map = new HashMap();
        map.put("begin", LocalDateTime.now().with(LocalTime.MIN));
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);

        //map对象改状态为待派送4 根据map查询数据库
        map.put("status", Orders.DELIVERY_IN_PROGRESS);
        Integer deliveredOrders = orderMapper.countByMap(map);
        //map对象改状态为已完成5 根据map查询数据库
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);
        //map对象改状态为已取消6 根据map查询数据库
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);
        //map对象改状态为null全部订单 根据map查询数据库
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);
        //封装返回OrderOverViewVO
        OrderOverViewVO orderOverViewVO = new OrderOverViewVO();
        orderOverViewVO.setAllOrders(allOrders);
        orderOverViewVO.setCancelledOrders(cancelledOrders);
        orderOverViewVO.setCompletedOrders(completedOrders);
        orderOverViewVO.setDeliveredOrders(deliveredOrders);
        orderOverViewVO.setWaitingOrders(waitingOrders);

        return orderOverViewVO;

    }
    //查询菜品总览
    @Override
    public DishOverViewVO getDishOverViewVO() {

        //新建map对象,map对象放入状态1,查询数据库启用菜品
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = dishMapper.countByMap(map);
        //map对象改状态0,查询数据库禁用菜品
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = dishMapper.countByMap(map);

        //封装返回DishOverViewVO
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public SetmealOverViewVO getSetmealOverViewVO() {

        //新建map对象,map对象放入状态1,查询数据库启用套餐
        Map map = new HashMap();
        map.put("status", StatusConstant.ENABLE);
        Integer sold = setmealMapper.countByMap(map);
        //map对象改状态0,查询数据库禁用菜品
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = setmealMapper.countByMap(map);

        //封装返回DishOverViewVO
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}

